package org.example.managementsystem.service;


import org.example.managementsystem.model.Product_Plan;
import org.example.managementsystem.model.Production_Submission;
import org.example.managementsystem.model.Production_Review;
import org.example.managementsystem.repository.Product_PlanDao;
import org.example.managementsystem.service.Production_SubmissionService;
import org.example.managementsystem.service.Production_ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class Product_PlanService {
    private final Product_PlanDao productPlanDao;

    @Autowired
    private Production_SubmissionService submissionService;

    @Autowired
    private Production_ReviewService reviewService;

    @Autowired
    public Product_PlanService(Product_PlanDao productPlanDao) {
        this.productPlanDao = productPlanDao;
    }

    public Product_Plan addProduct_Plan(Product_Plan product_plan) {
        Long id = productPlanDao.insertProduct_Plan(product_plan);
        product_plan.setPlan_id(id);
        return product_plan;
    }


    public boolean updateProduct_Plan(Product_Plan product_plan) {
        return productPlanDao.updateProduct_Plan(product_plan) > 0;
    }


    public boolean deleteProduct_Plan(Long plan_id) {
        return productPlanDao.deleteProduct_Plan(plan_id) > 0;
    }


    public Product_Plan getProduct_PlanById(Long plan_id) {
        return productPlanDao.getProduct_PlanById(plan_id);
    }


    public List<Product_Plan> getAllProduct_Plans() {
        return productPlanDao.getAllProduct_Plans();
    }

    /**
     * 计算计划完成情况
     */
    public Map<String, Object> calculatePlanCompletion() {
        // 获取所有计划
        List<Product_Plan> allPlans = getAllProduct_Plans();

        // 获取所有提交记录
        List<Production_Submission> allSubmissions = submissionService.getAllProduction_Submissions();

        // 获取所有审核记录
        List<Production_Review> allReviews = reviewService.getAllProduction_Reviews();

        // 统计每个部门的完成情况
        Map<String, Integer> departmentCompletion = allPlans.stream()
                .collect(Collectors.groupingBy(plan -> plan.getDepartment(), Collectors.summingInt(plan -> {
                    long approvedSubmissions = allSubmissions.stream()
                            .filter(submission -> submission.getPlan_id() == plan.getPlan_id())
                            .mapToInt(submission -> allReviews.stream()
                                    .filter(review -> review.getSubmission_id() == submission.getSubmission_id() && review.isIs_approved())
                                    .count() > 0 ? 1 : 0)
                            .sum();
                    return (int) approvedSubmissions;
                })));

        // 计算整体完成情况
        int totalCompleted = departmentCompletion.values().stream().mapToInt(Integer::intValue).sum();

        // 统计每个员工的完成情况
        Map<String, Integer> employeeCompletion = allSubmissions.stream()
                .collect(Collectors.groupingBy(Production_Submission::getSubmitter_job_number, Collectors.summingInt(submission -> allReviews.stream()
                        .filter(review -> review.getSubmission_id() == submission.getSubmission_id() && review.isIs_approved())
                        .count() > 0 ? 1 : 0)));

        // 返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("departmentCompletion", departmentCompletion);
        result.put("totalCompleted", totalCompleted);
        result.put("employeeCompletion", employeeCompletion);
        return result;
    }
}

